import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class LineChartSampleZhe1 extends StatefulWidget {
  int typeValue; // 0预热 3均热
  LineChartSampleZhe1({
    super.key,
    // required List<String> xTitlele = true,
    required this.xTitle,
    required this.listData,
    required this.typeValue,
  });

  final List<String> xTitle; // 更新参数类型为非空
  final List<dynamic> listData;

  @override
  State<LineChartSampleZhe1> createState() => _LineChartSampleZhe1State();
}

class _LineChartSampleZhe1State extends State<LineChartSampleZhe1> {
  List<Color> gradientColors = [
    Colors.white,
    Colors.blue,
  ];
  List<int> xRate = [];
  bool showAvg = false;
  String value = '';
  String value1 = '';

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    xRate = List<int>.generate(widget.listData.length,
        (index) => (index * (100 / (widget.listData.length - 1))).toInt());
    if (widget.typeValue == 1) {
      value = '';
      value1 = '';
    }
    if (widget.typeValue == 2) {
      value = '';
      value1 = '';
    }
    if (widget.typeValue == 3) {
      value = '';
      value1 = '';
    }
    print('=========');
    print(xRate);
    print('=========');

    return Stack(
      children: <Widget>[
        AspectRatio(
          aspectRatio: 1.70,
          child: Padding(
            padding: const EdgeInsets.only(top: 10, right: 30, left: 20),
            child: LineChart(
              mainData(),
            ),
          ),
        ),
        // SizedBox(
        //   width: 60,
        //   height: 34,
        //   child: TextButton(
        //     onPressed: () {
        //       setState(() {
        //         showAvg = !showAvg;
        //       });
        //     },
        //     child: Text(
        //       'avg',
        //       style: TextStyle(
        //         fontSize: 12,
        //         color: Colors.white,
        //       ),
        //     ),
        //   ),
        // ),
      ],
    );
  }

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 11,
      color: Color(0xFF65CEFF),
    );
    Widget text = const Text('', style: style);
    for (int i = 0; i < xRate.length; i++) {
      if (value.toInt() == xRate[i]) {
        if (widget.xTitle.length == xRate.length) {
          text = Text(widget.xTitle[i], style: style);
        } else {
          print("折线图-X轴与数据不匹配");
        }
        break;
      }
    }
    return SideTitleWidget(
      axisSide: meta.axisSide,
      child: text,
    );
  }

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontSize: 11,
      color: Colors.grey,
    );
    String text;
    switch (value.toInt()) {
      case 0:
        text = '300';
        break;
      case 10:
        text = '550';
        break;
      case 20:
        text = '800';
        break;
      case 30:
        text = '1050';
        break;
      case 40:
        text = '1300';
        break;
      default:
        return Container();
    }

    return Text(text, style: style, textAlign: TextAlign.left);
  }

  LineChartData mainData() {
    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: 1,
        verticalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return const FlLine(
            color: Colors.grey,
            strokeWidth: 0,
          );
        },
        getDrawingVerticalLine: (value) {
          return const FlLine(
            color: Colors.grey,
            strokeWidth: 0,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: true,
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            reservedSize: 30,
            interval: 1,
            getTitlesWidget: bottomTitleWidgets,
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            interval: 1,
            getTitlesWidget: leftTitleWidgets,
            reservedSize: 42,
          ),
        ),
      ),
      extraLinesData: ExtraLinesData(
        horizontalLines: [
          HorizontalLine(
            y: 1.8, // 限制Y轴的地址信息
            color: Colors.red, // 虚线颜色
            strokeWidth: 1, // 虚线高度
            dashArray: [20, 10],
          ),
        ],
      ),
      borderData: FlBorderData(
        show: false, // 隐藏边框--外边框
        border: Border.all(color: const Color(0xff37434d)),
      ),
      minX: 0,
      maxX: 100,
      minY: 0,
      maxY: 40,
      lineBarsData: [
        LineChartBarData(
          // spots: const [
          //   FlSpot(0, 20),
          //   FlSpot(20, 32),
          //   FlSpot(40, 35),
          //   FlSpot(60, 23),
          //   FlSpot(80, 14),
          //   FlSpot(100, 33),
          // ],
          spots: List<FlSpot>.generate(widget.listData.length, (index) {
            return FlSpot(
                index * (100 / (widget.listData.length - 1)),
                (widget.listData[index]['preDownRightTempA'].toDouble() +
                        widget.listData[index]['preDownLeftTempA'].toDouble()) /
                    30);
          }),
          isCurved: true,
          gradient: LinearGradient(
            colors: [Colors.blue, Colors.blue],
          ),
          barWidth: 1,

          /// 顶部线条宽度
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: true,
          ),
        ),
        LineChartBarData(
          // spots: const [
          //   FlSpot(0, 13),
          //   FlSpot(20, 35),
          //   FlSpot(40, 22),
          //   FlSpot(60, 31),
          //   FlSpot(80, 18),
          //   FlSpot(100, 32),
          // ],
          spots: List<FlSpot>.generate(widget.listData.length, (index) {
            return FlSpot(
                index * (100 / (widget.listData.length - 1)),
                (widget.listData[index]['preDownRightTempA'].toDouble() +
                        widget.listData[index]['preDownLeftTempA'].toDouble()) /
                    30);
          }),
          isCurved: true,
          gradient: LinearGradient(
            colors: [Colors.orange, Colors.orange],
          ),
          barWidth: 1,

          /// 顶部线条宽度
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: true,
          ),
        ),
      ],
    );
  }
}
